package com.lancoo.answer.widget.combinationView;

import android.content.Context;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.TaskState;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.util.ExamFontSizeAdjustUtils;

import java.util.List;

/**
 * Created by pengmi on 2021/12/22 18:33
 * Description:小题统计view
 */
public class ChildStatisticsView extends RelativeLayout {

    private TextView tv_totalChild;
    private Ques ques;

    public ChildStatisticsView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public ChildStatisticsView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ChildStatisticsView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.ev_layout_child_statistics, this);
        tv_totalChild = findViewById(R.id.tv_total_child);
    }
 
    public void setQues(Ques ques) {
        this.ques = ques;
        updateChildIndex(0);
        View tv_alert = findViewById(R.id.tv_alert);
        String genreID = ques.getGenreID();
        if (ConstantBean.getTaskState() == TaskState.TRAINING &&
                genreID != null && (TextUtils.equals(genreID, "39") || TextUtils.equals(genreID, "41"))) {
            if (ques.getSpecialAnswerMethod() == 0) {
                List<Child> childList = ques.getChildList();
                if(childList==null){
                    return;
                }
                List<Item> itemList = childList.get(0).getItemList();
                if(itemList==null){
                    return;
                }
                Item item = itemList.get(0);
                if (item.getItemAskList() == null || item.getItemAskList().size() == 0) {
                    tv_alert.setVisibility(GONE);
                } else {
                    tv_alert.setVisibility(VISIBLE);
                }
            } else {
                tv_alert.setVisibility(GONE);
            }
        } else {
            tv_alert.setVisibility(GONE);
        }
    }


    public void updateChildIndex(int childIndex) {
        if (ques == null) {
            return;
        }
        List<Child> childList = ques.getChildList();
        if(childList==null){
            return;
        }
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        String genreID = ques.getGenreID();
        String typeNo = ques.getTypeNo();
        String specialFeatureCodes = ques.getSpecialFeatureCodes();
        if (TextUtils.equals("k", typeNo) && TextUtils.equals(specialFeatureCodes, "14")) {//匹配+简答
            if (childIndex == 0) {
                ssb.append("从下面选项中挑选合适的选项完成");
                List<Item> itemList = childList.get(childIndex).getItemList();
                if(itemList==null){
                    return;
                }
                Item startItem = itemList.get(0);
                Item endItem = itemList.get(itemList.size() - 1);
                SpannableString ss = new SpannableString(startItem.getItemSortIndex() + "-" + endItem.getItemSortIndex());
                ss.setSpan(new ForegroundColorSpan(0xff0099ff), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.append(ss);
            } else {
                ssb.append("完成第");
                List<Item> itemList = childList.get(childIndex).getItemList();
                if(itemList==null){
                    return;
                }
                Item item = itemList.get(0);
                SpannableString ss = new SpannableString(item.getItemSortIndex());
                ss.setSpan(new ForegroundColorSpan(0xff0099ff), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.append(ss);
            }
            ssb.append("小题");
            tv_totalChild.setText(ssb);

            return;
        }

        if (genreID != null && (TextUtils.equals(genreID, "15")) && childList.size() == 2) {
            ssb.append("从下面选项中挑选合适的选项完成");
            List<Item> itemList = childList.get(childIndex).getItemList();
            if(itemList==null){
                return;
            }
            Item startItem = itemList.get(0);
            Item endItem = itemList.get(itemList.size() - 1);
            SpannableString ss = new SpannableString(startItem.getItemSortIndex() + "-" + endItem.getItemSortIndex());
            ss.setSpan(new ForegroundColorSpan(0xff0099ff), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.append(ss);
            ssb.append("小题");
            tv_totalChild.setText(ssb);
            return;
        }

        if (genreID != null && (TextUtils.equals(genreID, "15") ||
                TextUtils.equals(genreID, "17") || TextUtils.equals(typeNo, "x"))) {
            ssb.append("从下面选项中挑选合适的选项");
            tv_totalChild.setText(ssb);
            return;
        }

        if (genreID != null && (TextUtils.equals(genreID, "65"))) {
            int size = childList.size();
            SpannableString ss = new SpannableString("" + size);
            ss.setSpan(new ForegroundColorSpan(0xff0099ff), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.append("本题含 ");
            ssb.append(ss);
            ssb.append(" 小题");
            tv_totalChild.setText(ssb);
            return;
        }
        if (childList.size() == 1) {
            Child child = childList.get(0);
            int size = child.getItemList() == null ? 1 : (child.getItemList()==null?0:child.getItemList().size());
            SpannableString ss = new SpannableString("" + size);
            ss.setSpan(new ForegroundColorSpan(0xff0099ff), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.append("本题含 ");
            ssb.append(ss);
            ssb.append(" 小题");
            tv_totalChild.setText(ssb);
            return;
        }
        ssb.append(String.format("本题含%s小题 ", childList.size()));
        if (childList.size() > 1) {
            SpannableString ss1 = new SpannableString("(");
            ss1.setSpan(new AbsoluteSizeSpan(13, true), 0, ss1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss1.setSpan(new ForegroundColorSpan(0xff666666), 0, ss1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.append(ss1);
            SpannableString ss2 = new SpannableString("" + (childIndex + 1));
            ss2.setSpan(new AbsoluteSizeSpan(13, true), 0, ss2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss2.setSpan(new ForegroundColorSpan(0xff0099ff), 0, ss2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.append(ss2);
            SpannableString ss3 = new SpannableString("/" + childList.size() + ")");
            ss3.setSpan(new AbsoluteSizeSpan(13, true), 0, ss3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss3.setSpan(new ForegroundColorSpan(0xff666666), 0, ss3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.append(ss3);
        }
        tv_totalChild.setText(ssb);
    }

    /**
     * 更新听力题作答倒计时（ 考试作答专用）
     *
     * @param second 倒计时秒数
     * @param isShow 是否显示倒计时
     */
    public void updateAnswerCountdown(int second, boolean isShow) {
        TextView tv_countdown = findViewById(R.id.tv_countdown);
        tv_countdown.setVisibility(isShow ? VISIBLE : INVISIBLE);
        tv_countdown.setText(String.format("%s'", second));
    }

    public void adjustFontSize(){
        TextView textView = findViewById(R.id.tv_total_child);
        TextView textView1 = findViewById(R.id.tv_alert);
        TextView textView2 = findViewById(R.id.tv_countdown);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(textView,15);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(textView1,13);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(textView2,14);
    }


}
